#include <stdio.h>
#include <iostream>
#include"../base.h"
using namespace std;
#define SIZE 13
void Initial(int UFset[]){
	for (int i = 0; i < SIZE; i++)
	{
		UFset[i] = -1;
	}
}
int Find(int S[],int x) {
	int root = x;
	while (S[root]>0) root = S[root];
	while (x!=root)
	{
		int temp = S[x];
		S[x] = root;
		x = temp;
	}
	
	return x;
}
void Union(int S[],int root1,int root2) {
	if (root1 == root2) return;
	if (S[root2] > S[root1]) {
		S[root1] += S[root2];
		S[root2] = root1;
	}
	else
	{
		S[root2] += S[root1];
		S[root1] = root2;
	}
	
	
}
void componenCount(int A[5][5]) {
	int S[5];
	for (int i = 0; i < 5; i++) S[i] = -1;
	printArr(S, 5);
	for (int i = 0; i < 5; i++)
	{
		for (int j = i; j < 5; j++)
		{
			if (A[i][j] > 0) {
				int x = Find(S, i);
				int y = Find(S, j);
				if (x!=y)
				{
					Union(S, x, y);
				}
			}
		}

	}
	printArr(S, 5);
	int num = 0;
	for (int i = 0; i < 5; i++)
	{
		if (S[i] <0) num++;
	}
	cout << num;
}

int main() {
	int UFset[SIZE];
	int A[5][5] = {
		{0,3,1,0,0},
		{1,0,4,0,0},
		{0,1,0,0,0},
		{0,0,1,0,2},
		{1,1,1,0,0}
	};
	componenCount(A);

	return -1;
}